package com.zhongfl.crm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhongfl.crm.enums.LxMessageType;
import com.zhongfl.crm.mapper.LxMessageLogMapper;
import com.zhongfl.crm.model.LxMessageLog;
import com.zhongfl.crm.request.LxMessageLogRequest;
import com.zhongfl.crm.service.LxMessageLogService;
import org.springframework.stereotype.Service;
import tech.heptagram.staff.cas.common.AssertUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 奈良鹿丸
 * @date 2018/9/30
 */
@Service
public class LxMessageLogServiceImpl implements LxMessageLogService {

    @Resource
    private LxMessageLogMapper lxMessageLogMapper;

    @Override
    public void save(LxMessageType lxMessageType, String relationNo, String content) {
        LxMessageLog lxMessageLog = new LxMessageLog();
        lxMessageLog.setType(lxMessageType);
        lxMessageLog.setRelationNo(relationNo);
        lxMessageLog.setContent(content);

        save(lxMessageLog);
    }

    /**
     * 保存一条消息日志
     *
     * @param lxMessageLog 消息日志数据对象
     */
    @Override
    public void save(LxMessageLog lxMessageLog) {
        AssertUtils.notNull(lxMessageLog.getRelationNo(),"单号为空");
        AssertUtils.notNull(lxMessageLog.getType(),"消息类型为空");

        lxMessageLogMapper.insert(lxMessageLog);

    }

    /**
     * 消息日志分页查询
     *
     * @param lxMessageLogRequest 消息日志查询对象
     * @return 消息日志数据对象集合
     */
    @Override
    public PageInfo<LxMessageLog> findByPage(LxMessageLogRequest lxMessageLogRequest) {
        PageHelper.startPage(lxMessageLogRequest.getPage(), lxMessageLogRequest.getPageSize());
        List<LxMessageLog> lxMessageLogs = lxMessageLogMapper.selectByPage(lxMessageLogRequest);

        return new PageInfo<>(lxMessageLogs);
    }

    /**
     * 重置消息记录状态
     *
     * @param id 消息记录id
     */
    @Override
    public void reset(Long id) {
        lxMessageLogMapper.resetById(id);
    }
}
